package hu.szyrt.bug;

import java.util.ArrayList;

import hu.szyrt.bug.connections.RegistrationConnection;
import hu.szyrt.bug.dialogs.BUGDialog;
import hu.szyrt.bug.login.LoginHandler;
import hu.szyrt.bug.utils.Utils;
import hu.szyrt.bug.utils.Validator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegistrationActivity extends Activity {

	Button cancelButton, registrationButton;
	EditText name, email, password, passwordAgain;
	
	BUGDialog dialog;
	
	LoginHandler loginHandler;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.registration);
		
		this.loginHandler = new LoginHandler(this);
		
		this.dialog = new BUGDialog(this);
		this.dialog.setTitle(R.string.errors);
		this.dialog.setPositiveButton(R.string.ok, null);
		
		name = (EditText)findViewById(R.id.name);		
		email = (EditText)findViewById(R.id.email);		
		password = (EditText)findViewById(R.id.password);		
		passwordAgain = (EditText)findViewById(R.id.passwordAgain);
		
		cancelButton = (Button)findViewById(R.id.cancelButton);
		cancelButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		
		registrationButton = (Button)findViewById(R.id.registrationButton);
		registrationButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				startRegistration();
			}
		});
		
	}	
	
	private void startRegistration(){
		
		// adatok levétele
		String name = this.name.getText().toString().trim();
		String email = this.email.getText().toString().trim();
		String password = this.password.getText().toString().trim();
		String passwordAgain = this.passwordAgain.getText().toString().trim();
		
		// validáció
		ArrayList<String> errors = new ArrayList<String>();
		if(!Validator.isValidName(name)) errors.add(getString(R.string.err_invalid_name_format));
		if(!Validator.isValidEmail(email)) errors.add(getString(R.string.err_invalid_email_address));
		if(!Validator.isValidPassword(password)) errors.add(getString(R.string.err_invalid_password));
		if(!password.equals(passwordAgain)) errors.add(getString(R.string.err_invalid_password_match));
		
		if(errors != null && errors.size() > 0){
			BUGDialog dialog = new BUGDialog(this);
			dialog.setTitle(R.string.errors);
			dialog.setMessage(Utils.join(errors, "\n"));
			dialog.setPositiveButton(R.string.ok, null);
			dialog.show();
		}else{		
			// Regisztráció indítása
			
			// TODO: progress, amikor el kezdi a belépést
			if(Utils.isInternetAvailable(this)){
				final RegistrationConnection rc = new RegistrationConnection();
				rc.setUserName(name);
				rc.setEmail(email);
				rc.setPassword(password);
				rc.setNotify(new RegistrationConnection.Notify() {
					public void onSuccess() {
						Toast.makeText(
								RegistrationActivity.this, 
								rc.getResultText(), 
								Toast.LENGTH_LONG).show();
						
						// Beléptet és visszalép
						loginHandler.setLoginUser(rc.getMemberID());
						if(loginHandler.isLoggedIn()){
							setResult(RESULT_OK);
							finish();
						}
					}
					
					public void onError() {
						BUGDialog dialog = new BUGDialog(RegistrationActivity.this);
						dialog.setTitle(R.string.errors);
						dialog.setMessage(rc.getErrorText() == null ? rc.getResultText() : rc.getErrorText());
						dialog.setPositiveButton(R.string.ok, null);
						dialog.show();					
					}
				});
				rc.start();
			}else{
				Toast.makeText(this, R.string.err_no_internet, Toast.LENGTH_LONG).show();
			}
		}
	}
	
}
